import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


class Love{
    public String from;
    public String to;
    public String message;
}
@WebServlet("/Love")
public class LoveServlet extends HttpServlet {

    //private List<Love> loveList=new ArrayList<>();

    //向服务器提交数据
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper objectMapper=new ObjectMapper();
        //把body的数据读取出来，解析成Love对象

        Love love=objectMapper.readValue(req.getInputStream(),Love.class);
        //此处通过简单粗暴的方式来保存
        //loveList.add(love);
        save(love);
        //状态码设定
        resp.setStatus(200);
    }

    //从服务器获取数据
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper objectMapper=new ObjectMapper();

        resp.setContentType("application/json;charset=utf8");
        //这个方法把Java对象转换为json字符串，并且把这个字符串写回到响应对象中
//        objectMapper.writeValue(resp.getWriter(),loveList);

        List<Love> loveList=load();
        //分开写
        //把Java对象变为json字符串
        String jsonResp=objectMapper.writeValueAsString(loveList);
        System.out.println("jsonResp: "+jsonResp);
        //写会到响应中
        resp.getWriter().write(jsonResp);
    }

    //往数据库存入信息
    private void save(Love love)
    {
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            //1.建立连接
            connection=DbUtil.getConnection();
            //2.编写mysql代码
            String sql="insert into message values(?,?,?)";
            statement=connection.prepareStatement(sql);
            statement.setString(1,love.from);
            statement.setString(2,love.to);
            statement.setString(3,love.message);

            //3.执行sql
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4.关闭连接
            DbUtil.close(connection,statement,null);
        }


    }

    //从数据取出所有信息
    private List<Love> load()
    {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        List<Love> loveList=new ArrayList<>();
        try {
            //1.建立连接
            connection=DbUtil.getConnection();
            //2.编写mysql代码
            String sql="select * from message";
            statement=connection.prepareStatement(sql);

            //3.执行sql
            resultSet= statement.executeQuery();
            //4.遍历结果
            while(resultSet.next())
            {
                Love love=new Love();
                love.from=resultSet.getString("from");
                love.to=resultSet.getString("to");
                love.message=resultSet.getString("message");
                loveList.add(love);


            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //5.保存数据，关闭连接
            DbUtil.close(connection,statement,resultSet);
        }
        return  loveList;
    }



}
